package cz.incad.psh.pshassign.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import cz.incad.psh.pshassign.client.res.Resources;
import static com.google.gwt.query.client.GQuery.$;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.xml.client.Document;
import static gwtquery.plugins.ui.Ui.Ui;

/**
 *
 * @author Alberto Hernandez
 */
public class MainPage extends Composite {

    static {
        Resources.INSTANCE.css().ensureInjected();
    }
    private static MainPageUiBinder uiBinder = GWT.create(MainPageUiBinder.class);
    @UiField
    HTMLPanel center;
    @UiField
    InvenioSearch invenioSearch;
    @UiField
    PSHResults pshResults;
    @UiField
    InvenioDocInfo docInfo;
    @UiField
    SplitLayoutPanel splitPanel;
    @UiField
    Header header;
    @UiField
    FlowPanel status;
    
    void clearPSHKeys() {
        docInfo.clear();
        pshResults.clear();
    }

    void setStrength(String strength) {
        pshResults.changeStrength(strength);
    }

    AssignProgressBar pgAssign;
    void initAssignProgress() {
        if(status.getWidgetIndex(pgAssign)==-1){
            status.add(pgAssign);
        }
        pgAssign.sched();
        if(pgAssign.isRunning){
            pgAssign.setVisible(true);
        }
    }

    IndexProgressBar pgIndex;
    void initIndexProgress() {
        if(status.getWidgetIndex(pgIndex)==-1){
            status.add(pgIndex);
        }
        pgIndex.sched();
    }

    FocusPanel selectedResult;
    void selectResult(FocusPanel rBox) {
        if(selectedResult!=null){
            selectedResult.removeStyleName(Resources.INSTANCE.css().select());
        }
        rBox.setStyleName(Resources.INSTANCE.css().select(), true);
        selectedResult = rBox;
    }

    interface MainPageUiBinder extends UiBinder<Widget, MainPage> {
    }

    public MainPage() {
        initWidget(uiBinder.createAndBindUi(this));
        this.addDomHandler(new DocClickHandler(), ClickEvent.getType());
    }
    
    class DocClickHandler implements ClickHandler{
        
        
        @Override
        public void onClick(ClickEvent event) {
            
            header.hideAdmin();
        }
        
    }

    public void getPSHKeys(String id) {
        pshResults.getKeys(id);
        docInfo.show(id);
    }
    
    @Override
    protected void onLoad(){
        pgAssign = new AssignProgressBar(this);
        initAssignProgress();
        pgIndex = new IndexProgressBar(this);
        initIndexProgress();
        $(".gwt-Anchor").as(Ui).button();
        String qp = Window.Location.getParameter("id");
        if(qp !=null){
            getPSHKeys(qp);
        }
    }
    
    public String getStringOption(String section, String option) throws Exception{
      return header.options.getString(section, option);
   }
    
   public double getDoubleOption(String section, String option) throws Exception{
      return header.options.getDouble(section, option);
   }
    
   public int getIntOption(String section, String option) throws Exception{
      return header.options.getInt(section, option);
   } 
}
